package cn.org.eshow.service.impl;

import cn.org.eshow.bean.query.ProductQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.ProductDao;
import cn.org.eshow.model.Product;
import cn.org.eshow.service.ProductCategoryManager;
import cn.org.eshow.service.ProductManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.util.Date;
import java.util.List;

@WebService(serviceName = "ProductService", endpointInterface = "cn.org.eshow.service.ProductManager")
@Service
public class ProductManagerImpl extends GenericManagerImpl<Product, Integer> implements ProductManager {

    @Autowired
    ProductDao productDao;
    @Autowired
    ProductCategoryManager productCategoryManager;

    public ProductManagerImpl() {
    }

    @Autowired
    public ProductManagerImpl(ProductDao productDao) {
        super(productDao);
        this.productDao = productDao;
    }

    @Override
    public List<Product> list(ProductQuery query) {
        return productDao.list(query);
    }

    @Override
    public Page<Product> search(ProductQuery query) {
        return productDao.search(query);
    }

    @Override
    public Product check(ProductQuery query) {
        List<Product> list = productDao.list(query);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    public Long count(ProductQuery query) {
        return productDao.count(query);
    }

    @Override
    public Product updateProduct(Product old, Product product) {
        old.setUpdateTime(new Date());
        old.setImgs(!StringUtils.isEmpty(product.getImgs()) ? product.getImgs() : old.getImgs());
        old.setStyle(product.getStyle() != null ? product.getStyle() : old.getStyle());
        old.setName(product.getName() != null ? product.getName() : old.getName());
        old.setAge(product.getAge() != null ? product.getAge() : old.getAge());
        old.setPrice(product.getPrice() != null ? product.getPrice() : old.getPrice());
        old.setCostPrice((product.getCostPrice() != null&& product.getCostPrice() != 0)  ? product.getCostPrice() : old.getCostPrice());
        old.setMarketPrice((product.getMarketPrice() != null && product.getMarketPrice() != 0) ? product.getMarketPrice() : old.getMarketPrice());
        old.setImg(product.getImg() != null && !product.getImg().isEmpty() ? product.getImg() : old.getImg());
        old.setDescription(product.getDescription() != null ? product.getDescription() : old.getDescription());
        old.setContent(StringUtils.isNotEmpty(product.getContent()) ? product.getContent() : old.getContent());
        old.setState(product.getState() != null ? product.getState() : old.getState());
        if (product.getProductCategory() != null && product.getProductCategory().getId() != null) {
            old.setProductCategory(productCategoryManager.get(product.getProductCategory().getId()));
        }
        return productDao.save(old);
    }

    @Override
    public void updateFavoriteSize(Product product, Integer addSize) {
        product.setUpdateTime(new Date());
        if (product.getFavoriteSize() == null) {
            product.setFavoriteSize(0);
        }
        Integer result = product.getFavoriteSize() + addSize;
        if (result < 0) {
            product.setFavoriteSize(0);
        }
        productDao.save(product);
    }


}